package depot;

public class Name {

  private String firstName;
  private String middleName;
  private String lastName;
  
  /**
   * constructor to create name from full name
     in the format first name then space then last name
     or first name then space then middle name then space then last name
   * @param fullName check the formal of name
   * @throws NameValueException check the value of name
   */
  
  public Name (String fullName) throws NameValueException {
		
	  if(isCharacter(fullName)){
	  
	  int spacePos1 = fullName.indexOf(' ');
	  firstName = fullName.substring(0, spacePos1);
	  int spacePos2 = fullName.lastIndexOf(' ');
	  if (spacePos1 == spacePos2)
		  middleName = "";
	  else 
		  middleName = fullName.substring(spacePos1+1, spacePos2);
	  lastName = fullName.substring(spacePos2 + 1);
	  }
	  
	  else
			 throw new NameValueException(fullName);
      }
  
  /**
   * check whether the value of name is character
   * @param fullName the full name of customer
   * @return boolean if the value of name is fine, return true, else false
   */
  public boolean isCharacter(String fullName){
		 for (int i=0;i<=fullName.length();i++){
			 if((fullName.charAt(i)>='A' && fullName.charAt(i)<='Z') || (fullName.charAt(i)>='a' && fullName.charAt(i)<='z'))
					 return true;
		 }
		return false;
		
	 }
  
  /**
   * returns the full name, either first name then space then last name
     or first name then space then middle name then space and then last name
   * @return the full name 
   */
  
  public String getFullName() {
	  String result = firstName + " ";
	  if (!middleName.equals("")) {
	    	result += middleName + " ";
	  }
	  result += lastName;
	  return result;
	 }
  
  
  }
  
